/* $Id:stat_amount.cc $ */
/*
   Copyright (C) 2007 by Jurjen Stellingwerff <jurjen@stwerff.xs4all.nl>
   Part of the Moros Project http://moros.sourceforge.net/

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License version 3.
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY.

   See the COPYING file for more details.
*/

#define DEBUG_ROUTINE "stat_amount"

#include <sstream>
#include <fstream>
#include "../general/util.h"
#include "../general/log.h"
#include "../general/channel.h"
#include "stat_amount.h"
#include "stat.h"
#include "../general/general.h"

stat_amount::stat_amount() {
    stat=0;
    amount=0;
}

// Field : stat

class stat& stat_amount::getStat() const { if (stat==0) throw nullpointer(); return *stat; }
void stat_amount::setStat(class stat &v) { stat=&v; }

// Field : amount

const int stat_amount::getAmount() const { return amount; }
void stat_amount::setAmount(const int v) { amount=v; }

void stat_amount::save(class channel &ch) {
    ch.write("stat_amount", "");
    ch.write("s", getStat().getName());
    ch.write("a", getAmount());
}

void stat_amount::load(class channel &ch) {
    if (ch.key=="s" || ch.key=="stat") {
        if (!stat_repo.isStat(ch.value)) {
            dbg_state("link unknown stat %s", ch.value.c_str());
            class stat rec;
            rec.setName(ch.value);
            stat_repo.setStat(rec);
        }
        setStat(stat_repo.getStat(ch.value));
        ch.next();
    }
    if (ch.key=="a" || ch.key=="amount") { setAmount(atoi(ch.value.c_str())); ch.next(); }
}

void stat_amount::xml(ofstream &f, int l, int r) {
    f << ncpy(l, "  ") << "<stat_amount amount=\"" << getAmount() <<
    "\">" << endl;
    if (r<2) try {
        getStat().xml(f,l+1,r+1);
    } catch (nullpointer n) {}
    f << ncpy(l, "  ") << "</stat_amount>" << endl;
}
